AXIL_LOCSTR_TITLE = "Auxilliary Information Logger";
AXIL_LOCSTR_VERSION = "v0.1";
AXIL_LOCSTR_CMD_MAIN = "AXIL";

------------------------------------------------------------------------------------------

local function LogMessage(msg)
	local id = 0;
	local name = nil;
	id, name = GetChannelName(AXIL_Settings["LogChannel"]);
	if(nil == name) then
		JoinChannelByName(AXIL_Settings["LogChannel"])
	end
	SendChatMessage(FilterNull(msg), "CHANNEL", "Common", id);
	
	-- This is useful in debugging to see when items are linked
	--SendChatMessage(FilterNull(msg), "WHISPER", "Common", UnitName("player"));
end

------------------------------------------------------------------------------------------

local function DebugMessage(n,msg)
	local id = 0;
	local name = nil;
	id, name = GetChannelName(AXIL_Settings["DebugChannel"] .. n);
	SendChatMessage(FilterNull(msg), "CHANNEL", "Common", id);
end

------------------------------------------------------------------------------------------

local timerData = {};
function AXIL_ResetTimer(timerID)
	timerData[timerID] = nil;
end

------------------------------------------------------------------------------------------

local timerData = {};
function AXIL_Timer(timerID, expirationInSeconds)
	if ((nil == timerData[timerID]) or ((time() - timerData[timerID]) > expirationInSeconds)) then
		timerData[timerID] = time();
		return false;
	end
	return true;

end

------------------------------------------------------------------------------------------

function FilterNull(msg)
	if(nil == msg) then
		return 'nil';
	end
	if("" == msg) then
		return '<String.Empty>';
	end
	return msg;
end

------------------------------------------------------------------------------------------

function AXIL_DisplayMessage(msg)
	if(nil == msg) then
		return;
	end
	DEFAULT_CHAT_FRAME:AddMessage(msg, 0.5, 0.7, 0.3);
end

------------------------------------------------------------------------------------------

function AXIL_GroupIDs()
	local ids = {};
	local tempUnitName = nil;
	if(0 ~= GetNumRaidMembers()) then
		--local raidNumber = 40
		local raidNumber = GetNumRaidMembers();
		for i = 1, raidNumber do 
			if(UnitExists('raid'..i)) then
			ids['raid'..i] = i
				if(UnitExists('raidpet'..i)) then
					ids['raidpet'..i] = i
				end
			end
		end
	elseif(0 ~= GetNumPartyMembers()) then
		ids['player'] = 0
		if(UnitExists('pet')) then
			ids['pet'] = 0
		end
		--local raidNumber = 4
		local raidNumber = GetNumPartyMembers();
		for i= 0, raidNumber do
			for i = 1, raidNumber do 
				if(UnitExists('party'..i)) then
				ids['party'..i] = i
					if(UnitExists('partypet'..i)) then
						ids['partypet'..i] = i
					end
				end
			end
		end
	else
		ids['player'] = 0
		if(UnitExists('pet')) then
			ids['pet'] = 0
		end
	end
	if(UnitExists('target')) then
		ids['target'] = -1;
	end
	return ids
end

local function UnitExists(unit)
	if(nil == UnitName(unit)) then
		return false;
	end
	return true;
end

------------------------------------------------------------------------------------------

local queueMessageIndexStart = 0;
local queueMessageIndexEnd = 0;
local queueMessageData = {};

function AXIL_QueueMessage(debugLevel, msg)
	if(nil == msg) then
		return;
	end
	if(nil == debugLevel) then
		return;
	end
	queueMessageData[queueMessageIndexEnd] = {debugLevel, "[" .. time() .. "] " .. msg};
	queueMessageIndexEnd = queueMessageIndexEnd + 1;
end

function AXIL_DisplayQueuedMessages(n)
	if(queueMessageIndexStart + n > queueMessageIndexEnd) then
		AXIL_DisplayQueuedMessages(queueMessageIndexEnd - queueMessageIndexStart);
		return;
	end
	for i = queueMessageIndexStart,queueMessageIndexStart+n-1,1 do
		if(nil == queueMessageData[i]) then
			return;
		end
		debugLevel = queueMessageData[i][1];
		msg = queueMessageData[i][2];
		if(0 == debugLevel) then
			LogMessage(msg);
		else
			DebugMessage(debugLevel, msg);
		end
		queueMessageData[i] = nil;
	end
	queueMessageIndexStart = queueMessageIndexStart + n;
end

------------------------------------------------------------------------------------------

-- ****************
-- * AuxInfoLog   *
-- ****************

local AXIL_Initialized = false;

AXIL_Settings = 
{
	Logging = false,
	Debug = false,
	LogChannel = "AXIL",
	DebugChannel = "AXILDebug",
};

AuxInfoLogDetails = {
	name = 'AXIL',
	version = '0.1',
	releaseDate = 'June 30, 2007',
	author = 'Kohist',
	email = 'kohist@gmail.com',
	category = MYADDONS_CATEGORY_OTHERS
};

AXIL_AutoAuctionPostData = {};

------------------------------------------------------------------------------------------
-----------------   Raid Info    ---------------------------------------------------------
------------------------------------------------------------------------------------------

local raidSubGroups = {};
local raidLocations = {};
local function LogRaidInfo()

	AXIL_QueueMessage(1,"SCANNING RAID INFO");
	
	for i = 1,GetNumRaidMembers() do
		local name, rank, subgroup, level, class, fileName, zone, online, isDead = GetRaidRosterInfo(i);
		if(nil ~= name) then
			if(nil == raidSubGroups[name] or subgroup ~= raidSubGroups[name]) then
				raidSubGroups[name] = subgroup;
				AXIL_QueueMessage(0,"RAID INFO: Name = " .. name .. "; Group = " .. subgroup);
			end
			if(nil == raidLocations[name] or zone ~= raidLocations[name]) then
				raidLocations[name] = zone;
				AXIL_QueueMessage(0,"RAID INFO: Name = " .. name .. "; Zone = " .. zone);
			end
		end
	end
end

------------------------------------------------------------------------------------------

local freq_PeriodicallyLogRaidInfo = 5;
local function PeriodicallyLogRaidInfo()
	if(false == AXIL_Timer("PeriodicallyLogRaidInfo", freq_PeriodicallyLogRaidInfo)) then
		LogRaidInfo();
	end
end

------------------------------------------------------------------------------------------
-----------------   Guild Info    ---------------------------------------------------------
------------------------------------------------------------------------------------------

local guildInfo = {};
local function UnNull(msg)
	if(nil == msg) then
		return "nil";
	else
		return string.gsub(msg,"|","||");
	end
end
local function LogGuildInfo()
	if(nil == AXIL_Settings["guildInfo"]) then
		AXIL_Settings["guildInfo"] = {};
	end
	GuildRoster();
	--AXIL_DisplayMessage("SCANNING GUILD INFO: GetNumGuildMembers = " .. GetNumGuildMembers(true));
	
	for i = 1,GetNumGuildMembers(true) do

		--AXIL_DisplayMessage("SCANNING GUILD INFO: " .. i);

		local thisGuildInfo = {};
		
		if(nil ~= GetGuildRosterInfo(i)) then
			thisGuildInfo["name"], thisGuildInfo["rank"], thisGuildInfo["rankIndex"], 
				thisGuildInfo["level"], thisGuildInfo["class"], thisGuildInfo["zone"], 
				thisGuildInfo["note"], thisGuildInfo["officernote"], thisGuildInfo["online"], 
				thisGuildInfo["status"] = GetGuildRosterInfo(i);
			
			thisGuildInfo["yearsOffline"], thisGuildInfo["monthsOffline"], thisGuildInfo["daysOffline"], 
				thisGuildInfo["hoursOffline"] = GetGuildRosterLastOnline(i);
			
			if(nil ~= thisGuildInfo["name"]) then
					
				--AXIL_DisplayMessage("SCANNING GUILD INFO: " .. thisGuildInfo["name"]);
					
				if(nil == AXIL_Settings["guildInfo"][thisGuildInfo["name"]]) then
					AXIL_Settings["guildInfo"][thisGuildInfo["name"]] = thisGuildInfo;
					AXIL_QueueMessage(0,"GUILD INFO: Name = " .. thisGuildInfo["name"] .. "; Zone = " .. UnNull(thisGuildInfo["zone"]));
					AXIL_QueueMessage(0,"GUILD INFO: Name = " .. thisGuildInfo["name"] .. "; Note = " .. UnNull(thisGuildInfo["note"]));
					AXIL_QueueMessage(0,"GUILD INFO: Name = " .. thisGuildInfo["name"] .. "; Officer Note = " .. UnNull(thisGuildInfo["officernote"]));
				else
					if(AXIL_Settings["guildInfo"][thisGuildInfo["name"]]["zone"] ~= thisGuildInfo["zone"]) then
						AXIL_QueueMessage(0,"GUILD INFO: Name = " .. thisGuildInfo["name"] .. "; Zone = " .. UnNull(thisGuildInfo["zone"]));
					end
					if(AXIL_Settings["guildInfo"][thisGuildInfo["name"]]["note"] ~= thisGuildInfo["note"]) then
						AXIL_QueueMessage(0,"GUILD INFO: Name = " .. thisGuildInfo["name"] .. "; Note = " .. UnNull(thisGuildInfo["note"]));
					end
					if(AXIL_Settings["guildInfo"][thisGuildInfo["name"]]["officernote"] ~= thisGuildInfo["officernote"]) then
						AXIL_QueueMessage(0,"GUILD INFO: Name = " .. thisGuildInfo["name"] .. "; Officer Note = " .. UnNull(thisGuildInfo["officernote"]));
					end
					AXIL_Settings["guildInfo"][thisGuildInfo["name"]] = thisGuildInfo;
				end
			end
		end
	end
end

------------------------------------------------------------------------------------------

local freq_PeriodicallyLogGuildInfo = 15;
local function PeriodicallyLogGuildInfo()
	--AXIL_DisplayMessage("SCANNING GUILD INFO?");
	if(false == AXIL_Timer("PeriodicallyLogGuildInfo", freq_PeriodicallyLogGuildInfo)) then
		--AXIL_DisplayMessage("SCANNING GUILD INFO");
		LogGuildInfo();
	end
end

------------------------------------------------------------------------------------------
----------    Event Registration    ------------------------------------------------------
------------------------------------------------------------------------------------------

local function Register_For_Events()
	-- IF YOU ADD A NEW EVENT, ALSO ADD IT TO AXIL_Unregister_For_Events() !!!
end

------------------------------------------------------------------------------------------

local function Unregister_For_Events()
	-- IF YOU ADD A NEW EVENT, ALSO ADD IT TO Register_For_Events() !!!
end

------------------------------------------------------------------------------------------

local function Debug_Register_For_Events()
	-- IF YOU ADD A NEW EVENT, ALSO ADD IT TO AXIL_Debug_Unregister_For_Events() !!!
	this:RegisterEvent("UNIT_HEALTH");
	this:RegisterEvent("UNIT_MANA");
	this:RegisterEvent("UNIT_LEVEL");
	this:RegisterEvent("CHAT_MSG_COMBAT_CREATURE_VS_CREATURE_HITS");
	this:RegisterEvent("CHAT_MSG_COMBAT_CREATURE_VS_CREATURE_MISSES");
	this:RegisterEvent("CHAT_MSG_COMBAT_CREATURE_VS_PARTY_HITS");
	this:RegisterEvent("CHAT_MSG_COMBAT_CREATURE_VS_PARTY_MISSES");
	this:RegisterEvent("CHAT_MSG_COMBAT_CREATURE_VS_SELF_HITS");
	this:RegisterEvent("CHAT_MSG_COMBAT_CREATURE_VS_SELF_MISSES");
	this:RegisterEvent("CHAT_MSG_COMBAT_FRIENDLYPLAYER_HITS");
	this:RegisterEvent("CHAT_MSG_COMBAT_FRIENDLYPLAYER_MISSES");
	this:RegisterEvent("CHAT_MSG_COMBAT_FRIENDLY_DEATH");
	this:RegisterEvent("CHAT_MSG_COMBAT_HOSTILEPLAYER_HITS");
	this:RegisterEvent("CHAT_MSG_COMBAT_HOSTILEPLAYER_MISSES");
	this:RegisterEvent("CHAT_MSG_COMBAT_HOSTILE_DEATH");
	this:RegisterEvent("CHAT_MSG_COMBAT_PARTY_HITS");
	this:RegisterEvent("CHAT_MSG_COMBAT_PARTY_MISSES");
	this:RegisterEvent("CHAT_MSG_SPELL_AURA_GONE_OTHER");
	this:RegisterEvent("CHAT_MSG_SPELL_AURA_GONE_SELF");
	this:RegisterEvent("CHAT_MSG_SPELL_BREAK_AURA");
	this:RegisterEvent("CHAT_MSG_SPELL_CREATURE_VS_CREATURE_BUFF");
	this:RegisterEvent("CHAT_MSG_SPELL_CREATURE_VS_CREATURE_DAMAGE");
	this:RegisterEvent("CHAT_MSG_SPELL_CREATURE_VS_PARTY_BUFF");
	this:RegisterEvent("CHAT_MSG_SPELL_CREATURE_VS_PARTY_DAMAGE");
	this:RegisterEvent("CHAT_MSG_SPELL_CREATURE_VS_SELF_BUFF");
	this:RegisterEvent("CHAT_MSG_SPELL_CREATURE_VS_SELF_DAMAGE");
	this:RegisterEvent("CHAT_MSG_SPELL_DAMAGESHIELDS_ON_OTHERS");
	this:RegisterEvent("CHAT_MSG_SPELL_DAMAGESHIELDS_ON_SELF");
	this:RegisterEvent("CHAT_MSG_SPELL_FAILED_LOCALPLAYER");
	this:RegisterEvent("CHAT_MSG_SPELL_FRIENDLYPLAYER_BUFF");
	this:RegisterEvent("CHAT_MSG_SPELL_FRIENDLYPLAYER_DAMAGE");
	this:RegisterEvent("CHAT_MSG_SPELL_HOSTILEPLAYER_BUFF");
	this:RegisterEvent("CHAT_MSG_SPELL_HOSTILEPLAYER_DAMAGE");
	this:RegisterEvent("CHAT_MSG_SPELL_PARTY_BUFF");
	this:RegisterEvent("CHAT_MSG_SPELL_PARTY_DAMAGE");
	this:RegisterEvent("CHAT_MSG_SPELL_PERIODIC_CREATURE_BUFFS");
	this:RegisterEvent("CHAT_MSG_SPELL_PERIODIC_CREATURE_DAMAGE");
	this:RegisterEvent("CHAT_MSG_SPELL_PERIODIC_FRIENDLYPLAYER_BUFFS");
	this:RegisterEvent("CHAT_MSG_SPELL_PERIODIC_FRIENDLYPLAYER_DAMAGE");
	this:RegisterEvent("CHAT_MSG_SPELL_PERIODIC_HOSTILEPLAYER_BUFFS");
	this:RegisterEvent("CHAT_MSG_SPELL_PERIODIC_HOSTILEPLAYER_DAMAGE");
	this:RegisterEvent("CHAT_MSG_SPELL_PERIODIC_PARTY_BUFFS");
	this:RegisterEvent("CHAT_MSG_SPELL_PERIODIC_PARTY_DAMAGE");
	this:RegisterEvent("CHAT_MSG_SPELL_PERIODIC_SELF_BUFFS");
	this:RegisterEvent("CHAT_MSG_SPELL_PERIODIC_SELF_DAMAGE");
	this:RegisterEvent("CHAT_MSG_SPELL_PET_BUFF");
	this:RegisterEvent("CHAT_MSG_SPELL_PET_DAMAGE");
	this:RegisterEvent("CHAT_MSG_SPELL_SELF_BUFF");
	this:RegisterEvent("CHAT_MSG_SPELL_SELF_DAMAGE");
	this:RegisterEvent("SPELLCAST_CHANNEL_START");
	this:RegisterEvent("SPELLCAST_CHANNEL_UPDATE");
	this:RegisterEvent("SPELLCAST_DELAYED");
	this:RegisterEvent("SPELLCAST_FAILED");
	this:RegisterEvent("SPELLCAST_INTERRUPTED");
	this:RegisterEvent("SPELLCAST_START");
	this:RegisterEvent("SPELLCAST_STOP");
	this:RegisterEvent("UNIT_ATTACK_POWER");
	this:RegisterEvent("UNIT_ATTACK_SPEED");
	this:RegisterEvent("UNIT_AURA");
	this:RegisterEvent("UNIT_AURASTATE");
	this:RegisterEvent("UNIT_CLASSIFICATION_CHANGED");
	this:RegisterEvent("UNIT_COMBO_POINTS");
	this:RegisterEvent("UNIT_DEFENSE");
	this:RegisterEvent("UNIT_DISPLAYPOWER");
	this:RegisterEvent("UNIT_DYNAMIC_FLAGS");
	this:RegisterEvent("UNIT_ENERGY");
	this:RegisterEvent("UNIT_FACTION");
	this:RegisterEvent("UNIT_FLAGS");
	this:RegisterEvent("UNIT_FOCUS");
	this:RegisterEvent("UNIT_HAPPINESS");
	this:RegisterEvent("UNIT_INVENTORY_CHANGED");
	this:RegisterEvent("UNIT_LOYALTY");
	this:RegisterEvent("UNIT_MAXHAPPINESS");
	this:RegisterEvent("UNIT_MODEL_CHANGED");
	this:RegisterEvent("UNIT_NAME_UPDATE");
	this:RegisterEvent("UNIT_PET");
	this:RegisterEvent("UNIT_PET_EXPERIENCE");
	this:RegisterEvent("UNIT_PET_TRAINING_POINTS");
	this:RegisterEvent("UNIT_PORTRAIT_UPDATE");
	this:RegisterEvent("UNIT_PVP_UPDATE");
	this:RegisterEvent("UNIT_RANGED_ATTACK_POWER");
	this:RegisterEvent("UNIT_RESISTANCES");
	this:RegisterEvent("UNIT_SPELLMISS");
	this:RegisterEvent("UNIT_STATS");
	this:RegisterEvent("UNIT_MAXHEALTH");
	this:RegisterEvent("UNIT_MAXMANA");
	this:RegisterEvent("UNIT_MAXRAGE");
	this:RegisterEvent("UNIT_MAXENERGY");
	this:RegisterEvent("UNIT_MAXFOCUS");
	this:RegisterEvent("UNIT_RANGEDDAMAGE");
	this:RegisterEvent("UNIT_DAMAGE");
	this:RegisterEvent("UNIT_ATTACK");
	this:RegisterEvent("UNIT_COMBAT");
	this:RegisterEvent("UNIT_RAGE");
	this:RegisterEvent("PARTY_MEMBERS_CHANGED");
	this:RegisterEvent("RAID_ROSTER_UPDATE");
	this:RegisterEvent("CHAT_MSG_CHANNEL_LEAVE");
	this:RegisterEvent("CHAT_MSG_CHANNEL_JOIN");
	this:RegisterEvent("ZONE_CHANGED");
	this:RegisterEvent("ZONE_CHANGED_INDOORS");
	this:RegisterEvent("ZONE_CHANGED_NEW_AREA");
	this:RegisterEvent("CHAT_MSG_SYSTEM");
end

------------------------------------------------------------------------------------------

local function Debug_Unregister_For_Events()
	-- IF YOU ADD A NEW EVENT, ALSO ADD IT TO AXIL_Debug_Register_For_Events() !!!
	this:UnregisterEvent("UNIT_HEALTH");
	this:UnregisterEvent("UNIT_MANA");
	this:UnregisterEvent("UNIT_LEVEL");
	this:UnregisterEvent("CHAT_MSG_SYSTEM");
	this:UnregisterEvent("ZONE_CHANGED");
	this:UnregisterEvent("ZONE_CHANGED_INDOORS");
	this:UnregisterEvent("ZONE_CHANGED_NEW_AREA");
	this:UnregisterEvent("CHAT_MSG_CHANNEL_JOIN");
	this:UnregisterEvent("CHAT_MSG_CHANNEL_LEAVE");
	this:UnregisterEvent("CHAT_MSG_COMBAT_CREATURE_VS_CREATURE_HITS");
	this:UnregisterEvent("CHAT_MSG_COMBAT_CREATURE_VS_CREATURE_MISSES");
	this:UnregisterEvent("CHAT_MSG_COMBAT_CREATURE_VS_PARTY_HITS");
	this:UnregisterEvent("CHAT_MSG_COMBAT_CREATURE_VS_PARTY_MISSES");
	this:UnregisterEvent("CHAT_MSG_COMBAT_CREATURE_VS_SELF_HITS");
	this:UnregisterEvent("CHAT_MSG_COMBAT_CREATURE_VS_SELF_MISSES");
	this:UnregisterEvent("CHAT_MSG_COMBAT_FRIENDLYPLAYER_HITS");
	this:UnregisterEvent("CHAT_MSG_COMBAT_FRIENDLYPLAYER_MISSES");
	this:UnregisterEvent("CHAT_MSG_COMBAT_FRIENDLY_DEATH");
	this:UnregisterEvent("CHAT_MSG_COMBAT_HOSTILEPLAYER_HITS");
	this:UnregisterEvent("CHAT_MSG_COMBAT_HOSTILEPLAYER_MISSES");
	this:UnregisterEvent("CHAT_MSG_COMBAT_HOSTILE_DEATH");
	this:UnregisterEvent("CHAT_MSG_COMBAT_PARTY_HITS");
	this:UnregisterEvent("CHAT_MSG_COMBAT_PARTY_MISSES");
	this:UnregisterEvent("CHAT_MSG_SPELL_AURA_GONE_OTHER");
	this:UnregisterEvent("CHAT_MSG_SPELL_AURA_GONE_SELF");
	this:UnregisterEvent("CHAT_MSG_SPELL_BREAK_AURA");
	this:UnregisterEvent("CHAT_MSG_SPELL_CREATURE_VS_CREATURE_BUFF");
	this:UnregisterEvent("CHAT_MSG_SPELL_CREATURE_VS_CREATURE_DAMAGE");
	this:UnregisterEvent("CHAT_MSG_SPELL_CREATURE_VS_PARTY_BUFF");
	this:UnregisterEvent("CHAT_MSG_SPELL_CREATURE_VS_PARTY_DAMAGE");
	this:UnregisterEvent("CHAT_MSG_SPELL_CREATURE_VS_SELF_BUFF");
	this:UnregisterEvent("CHAT_MSG_SPELL_CREATURE_VS_SELF_DAMAGE");
	this:UnregisterEvent("CHAT_MSG_SPELL_DAMAGESHIELDS_ON_OTHERS");
	this:UnregisterEvent("CHAT_MSG_SPELL_DAMAGESHIELDS_ON_SELF");
	this:UnregisterEvent("CHAT_MSG_SPELL_FAILED_LOCALPLAYER");
	this:UnregisterEvent("CHAT_MSG_SPELL_FRIENDLYPLAYER_BUFF");
	this:UnregisterEvent("CHAT_MSG_SPELL_FRIENDLYPLAYER_DAMAGE");
	this:UnregisterEvent("CHAT_MSG_SPELL_HOSTILEPLAYER_BUFF");
	this:UnregisterEvent("CHAT_MSG_SPELL_HOSTILEPLAYER_DAMAGE");
	this:UnregisterEvent("CHAT_MSG_SPELL_PARTY_BUFF");
	this:UnregisterEvent("CHAT_MSG_SPELL_PARTY_DAMAGE");
	this:UnregisterEvent("CHAT_MSG_SPELL_PERIODIC_CREATURE_BUFFS");
	this:UnregisterEvent("CHAT_MSG_SPELL_PERIODIC_CREATURE_DAMAGE");
	this:UnregisterEvent("CHAT_MSG_SPELL_PERIODIC_FRIENDLYPLAYER_BUFFS");
	this:UnregisterEvent("CHAT_MSG_SPELL_PERIODIC_FRIENDLYPLAYER_DAMAGE");
	this:UnregisterEvent("CHAT_MSG_SPELL_PERIODIC_HOSTILEPLAYER_BUFFS");
	this:UnregisterEvent("CHAT_MSG_SPELL_PERIODIC_HOSTILEPLAYER_DAMAGE");
	this:UnregisterEvent("CHAT_MSG_SPELL_PERIODIC_PARTY_BUFFS");
	this:UnregisterEvent("CHAT_MSG_SPELL_PERIODIC_PARTY_DAMAGE");
	this:UnregisterEvent("CHAT_MSG_SPELL_PERIODIC_SELF_BUFFS");
	this:UnregisterEvent("CHAT_MSG_SPELL_PERIODIC_SELF_DAMAGE");
	this:UnregisterEvent("CHAT_MSG_SPELL_PET_BUFF");
	this:UnregisterEvent("CHAT_MSG_SPELL_PET_DAMAGE");
	this:UnregisterEvent("CHAT_MSG_SPELL_SELF_BUFF");
	this:UnregisterEvent("CHAT_MSG_SPELL_SELF_DAMAGE");
	this:UnregisterEvent("SPELLCAST_CHANNEL_START");
	this:UnregisterEvent("SPELLCAST_CHANNEL_UPDATE");
	this:UnregisterEvent("SPELLCAST_DELAYED");
	this:UnregisterEvent("SPELLCAST_FAILED");
	this:UnregisterEvent("SPELLCAST_INTERRUPTED");
	this:UnregisterEvent("SPELLCAST_START");
	this:UnregisterEvent("SPELLCAST_STOP");
	this:UnregisterEvent("UNIT_ATTACK_POWER");
	this:UnregisterEvent("UNIT_ATTACK_SPEED");
	this:UnregisterEvent("UNIT_AURA");
	this:UnregisterEvent("UNIT_AURASTATE");
	this:UnregisterEvent("UNIT_CLASSIFICATION_CHANGED");
	this:UnregisterEvent("UNIT_COMBO_POINTS");
	this:UnregisterEvent("UNIT_DEFENSE");
	this:UnregisterEvent("UNIT_DISPLAYPOWER");
	this:UnregisterEvent("UNIT_DYNAMIC_FLAGS");
	this:UnregisterEvent("UNIT_ENERGY");
	this:UnregisterEvent("UNIT_FACTION");
	this:UnregisterEvent("UNIT_FLAGS");
	this:UnregisterEvent("UNIT_FOCUS");
	this:UnregisterEvent("UNIT_HAPPINESS");
	this:UnregisterEvent("UNIT_INVENTORY_CHANGED");
	this:UnregisterEvent("UNIT_LOYALTY");
	this:UnregisterEvent("UNIT_MAXHAPPINESS");
	this:UnregisterEvent("UNIT_MODEL_CHANGED");
	this:UnregisterEvent("UNIT_NAME_UPDATE");
	this:UnregisterEvent("UNIT_PET");
	this:UnregisterEvent("UNIT_PET_EXPERIENCE");
	this:UnregisterEvent("UNIT_PET_TRAINING_POINTS");
	this:UnregisterEvent("UNIT_PORTRAIT_UPDATE");
	this:UnregisterEvent("UNIT_PVP_UPDATE");
	this:UnregisterEvent("UNIT_RANGED_ATTACK_POWER");
	this:UnregisterEvent("UNIT_RESISTANCES");
	this:UnregisterEvent("UNIT_SPELLMISS");
	this:UnregisterEvent("UNIT_STATS");
	this:UnregisterEvent("UNIT_MAXHEALTH");
	this:UnregisterEvent("UNIT_MAXMANA");
	this:UnregisterEvent("UNIT_MAXRAGE");
	this:UnregisterEvent("UNIT_MAXENERGY");
	this:UnregisterEvent("UNIT_MAXFOCUS");
	this:UnregisterEvent("UNIT_RANGEDDAMAGE");
	this:UnregisterEvent("UNIT_DAMAGE");
	this:UnregisterEvent("UNIT_ATTACK");
	this:UnregisterEvent("UNIT_COMBAT");
	this:UnregisterEvent("UNIT_RAGE");
	this:UnregisterEvent("PARTY_MEMBERS_CHANGED");
	this:UnregisterEvent("RAID_ROSTER_UPDATE");
end
------------------------------------------------------------------------------------------
---------------------     Basic Control And Display Functions   --------------------------
------------------------------------------------------------------------------------------

local function LogStartLogging()
	AXIL_QueueMessage(0,"AXIL now active. Character Name: " .. UnitName("player"));
end

------------------------------------------------------------------------------------------

local function LogLoginStartLogging()
	AXIL_QueueMessage(0,"AXIL has started logging due to startup setting.");
	LogStartLogging();
end

------------------------------------------------------------------------------------------

local function LogEndLogging()
	AXIL_QueueMessage(0,"AXIL has stopped logging.");
end

------------------------------------------------------------------------------------------

local function PrintStatus()
	AXIL_DisplayMessage("---- AXIL Current Status ----");
	if(AXIL_Settings["Logging"]) then
		AXIL_DisplayMessage("Logging is turned ON");
	else
		AXIL_DisplayMessage("Logging is turned OFF");
	end
	if(AXIL_Settings["Debug"]) then
		AXIL_DisplayMessage("Debug mode is turned ON");
	end
	AXIL_DisplayMessage("Debug channel prefix is " .. AXIL_Settings["DebugChannel"]);
	AXIL_DisplayMessage("Log channel is " .. AXIL_Settings["LogChannel"]);
end

------------------------------------------------------------------------------------------

local function PrintHelp()
	AXIL_DisplayMessage("---- AXIL Help ----");
	AXIL_DisplayMessage("/" .. AXIL_LOCSTR_CMD_MAIN .. " ON - Turns on logging");
	AXIL_DisplayMessage("/" .. AXIL_LOCSTR_CMD_MAIN .. " OFF - Turns off logging");
	AXIL_DisplayMessage("/" .. AXIL_LOCSTR_CMD_MAIN .. " DEBUG - Toggles debugging");
	AXIL_DisplayMessage("/" .. AXIL_LOCSTR_CMD_MAIN .. " DEBUGCHAN <channel_name> - Changes the debugging channel prefix");
	AXIL_DisplayMessage("/" .. AXIL_LOCSTR_CMD_MAIN .. " LOGCHAN <channel_name> - Changes the logging channel");
end

------------------------------------------------------------------------------------------

local function AddOnLoaded()
	if(AXIL_Initialized) then
		return;
	end

	if(AXIL_Settings["Logging"]) then
		Register_For_Events();
		LoggingChat(true);
		LoggingCombat(true);
		LogLoginStartLogging();
	end
	if(AXIL_Settings["Debug"]) then
		Debug_Register_For_Events();
	end
	PrintStatus();
	
	-- Announce the addon was loaded
	AXIL_DisplayMessage(AXIL_LOCSTR_TITLE .. " " .. AXIL_LOCSTR_VERSION .. " Loaded; use /" .. AXIL_LOCSTR_CMD_MAIN);
	AXIL_Initialized = true;
end

------------------------------------------------------------------------------------------
-----------------     Public API Hook Functions    ---------------------------------------
------------------------------------------------------------------------------------------

function AXIL_OnLoad()
	-- add the slash command
	SLASH_AXILMAIN1 = "/" .. AXIL_LOCSTR_CMD_MAIN;
	SLASH_AXILMAIN2 = "/" .. AXIL_LOCSTR_CMD_MAIN;
	SlashCmdList["AXILMAIN"] = AXIL_Command;
	this:RegisterEvent("ADDON_LOADED");
end

------------------------------------------------------------------------------------------

function AXIL_Command(msg)

	if (AXIL_Settings["Debug"]) then
		AXIL_QueueMessage(1,"AXIL_Command: " .. FilterNull(msg));
	end
	
	if("" == msg) then
		PrintStatus();
		PrintHelp();
	end
	
	if("ON" == string.upper(msg)) then
		if(false == AXIL_Settings["Logging"]) then
			AXIL_Settings["Logging"] = true;
			Register_For_Events();
			AXIL_DisplayMessage("AXIL turned ON");
			LoggingChat(true);
			LoggingCombat(true);
		else
			AXIL_DisplayMessage("AXIL already turned ON");
		end
	end

	if("OFF" == string.upper(msg)) then
		if(AXIL_Settings["Logging"]) then
			AXIL_Settings["Logging"] = false;
			Unregister_For_Events();
			AXIL_DisplayMessage("AXIL turned OFF");
			LeaveChannelByName(AXIL_Settings["LogChannel"]);
			LoggingChat(false);
			LoggingCombat(false);
		else
			AXIL_DisplayMessage("AXIL already turned OFF");
		end
	end

	if("DEBUG" == string.upper(msg)) then
		if(AXIL_Settings["Debug"]) then
			AXIL_DisplayMessage("AXIL Debug mode turned OFF");
			Debug_Unregister_For_Events();
			AXIL_Settings["Debug"] = false;
		else
			AXIL_DisplayMessage("AXIL Debug mode turned ON");
			Debug_Register_For_Events();
			AXIL_Settings["Debug"] = true;
		end
	end
	
	if(nil ~= string.find(string.upper(msg), "DEBUGCHAN")) then
		AXIL_Settings["DebugChannel"] = string.gsub(string.upper(msg), "DEBUGCHAN +", "");
		AXIL_DisplayMessage("AXIL Debug channel prefix changed to " .. AXIL_Settings["DebugChannel"]);
	end
	
	if(nil ~= string.find(string.upper(msg), "LOGCHAN")) then
		LeaveChannelByName(AXIL_Settings["LogChannel"]);
		AXIL_Settings["LogChannel"] = string.gsub(string.upper(msg), "LOGCHAN +", "");
		AXIL_DisplayMessage("AXIL Log channel changed to " .. AXIL_Settings["LogChannel"]);
		LogStartLogging();
	end
end

------------------------------------------------------------------------------------------

local isHandlingUpdate = false;
local updateInterval = 1; -- how often my OnUpdate code will run
local timeToRunNext = 0; -- how often my OnUpdate code will run
local numberOfQueuedMessagesToDisplayPerTick = 10; -- how often my OnUpdate code will run
function AXIL_OnUpdate(elapsed) -- elapsed is always nil... what the hell?

	--AXIL_DisplayMessage("Not time: " .. time() .. " / " .. timeToRunNext);

	if ((false == isHandlingUpdate) and (time() > timeToRunNext) and (true == AXIL_Settings["Logging"])) 
	then 
		--isHandlingUpdate = true;
		
		----------------------------------------------------------------------------------
		-- Begin OnUpdate code
		----------------------------------------------------------------------------------

		--AXIL_DisplayMessage("UPDATING INFO");

		AXIL_DisplayQueuedMessages(numberOfQueuedMessagesToDisplayPerTick);
		PeriodicallyLogRaidInfo();
		PeriodicallyLogGuildInfo();
		
		----------------------------------------------------------------------------------
		-- End OnUpdate code
		----------------------------------------------------------------------------------
		
		isHandlingUpdate = false;
		timeToRunNext = time() + updateInterval;
	
	end
end

------------------------------------------------------------------------------------------

function AXIL_OnEvent(event)
	if(event == "ADDON_LOADED") then
		AddOnLoaded();
		return;
	end

	if (AXIL_Settings["Debug"]) then
		local msg = "AXIL_OnEvent: ";
		if (event ~= nil) then
			msg = msg .. event;
		end
		if (arg1 ~= nil) then
			msg = msg .. " arg1: " .. arg1;
		end
		if (arg2 ~= nil) then
			msg = msg .. " arg2: " .. arg2;
		end
		if (arg3 ~= nil) then
			msg = msg .. " arg3: " .. arg3;
		end
		if (arg4 ~= nil) then
			msg = msg .. " arg4: " .. arg4;
		end
		if (arg5 ~= nil) then
			msg = msg .. " arg5: " .. arg5;
		end
		if (arg6 ~= nil) then
			msg = msg .. " arg6: " .. arg6;
		end
		if (arg7 ~= nil) then
			msg = msg .. " arg7: " .. arg7;
		end
		if (arg8 ~= nil) then
			msg = msg .. " arg8: " .. arg8;
		end
		if (arg9 ~= nil) then
			msg = msg .. " arg9: " .. arg9;
		end
		AXIL_QueueMessage(1,msg);
	end
end

